import { login, getUserInfo, getUserDetailById } from '@/api'

import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      // 将 token 存入 cookie
      setToken(newToken)
    },
    async setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    // setkkn(state) {
    //   state.kk = ' newToken'
    //   console.log('ada')
    // },
    unuse() {
      console.log(removeToken)
    }
  },
  actions: {
    async fetchLogin(ctx, data) {
      const res = await login(data)
      ctx.commit('setToken', res.data)
      return res
    },
    async fetchUserInfo(ctx) {
      const res = await getUserInfo()
      const res2 = await getUserDetailById(res.data.userId)
      ctx.commit('setUserInfo', { ...res.data, ...res2.data })
    }
  }
}
